package org.eae.eldf.webapp.controller.module;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eae.eldf.entity.module.ModuleCompany;
import org.eae.eldf.entity.module.ModuleInfo;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.service.module.ModuleCompanyService;
import org.eae.eldf.service.module.ModuleInfoService;
import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.webapp.common.bean.Result;
import org.eae.eldf.webapp.common.constant.StateDefine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/module")
public class ModuleCompanyController extends AbstractController<ModuleCompany> {

	private ModuleCompanyService moduleCompanyService;

	private ModuleInfoService moduleInfoService;

	@Autowired
	public void setModuleInfoService(ModuleInfoService moduleInfoService) {
		this.moduleInfoService = moduleInfoService;
	}

	@Autowired
	public void setModuleCompanyService(ModuleCompanyService moduleCompanyService) {
		this.moduleCompanyService = moduleCompanyService;
	}


	/**
	 * 查询
	 * 
	 * @param map
	 * @param searchValue
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/company/list")
	public String listPage(ModelMap map, @RequestParam(required = false) String searchValue, HttpServletRequest request,
			HttpServletResponse response) {
		Pagination<ModuleCompany> pagerPagination = this.initPager(request, response);
		pagerPagination = moduleCompanyService.queryListModuleCompany(pagerPagination, searchValue);
		map.addAttribute("list", pagerPagination);
		map.addAttribute("searchValue", searchValue);
		return "/module/company/list";
	}

	/**
	 * 通过conpanyCode查找moduleCompany并且跳转至分配module页面
	 * 
	 * @param modelMap
	 * @param id
	 * @param searchValue
	 * @return
	 */
	@RequestMapping(value = "/company/form")
	@ResponseBody
	public Map<String, List<ModuleInfo>> findByModuleCompanyByCompanyCode(HttpServletRequest request,
			HttpServletResponse response, @RequestParam(required = false) String companyCode) {
		Map<String, List<ModuleInfo>> data = new HashMap<>();
		List<ModuleInfo> companyHasModules = moduleInfoService.getCompanyHasModules(companyCode);
		List<ModuleInfo> companyNotHasModules = moduleInfoService.getCompanyNotHasModules(companyCode);
		data.put("companyHasModules", companyHasModules);
		data.put("companyNotHasModules", companyNotHasModules);
		return data;

	}

	/**
	 * 新加/修改company的module
	 * 
	 * @param modelAndView
	 * @param moduleCompanyId
	 * @param companyCode
	 * @param moduleInfoIds
	 * @param modelMap
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/companymodule/add")
	// @Token(remove=true,repeatSubmitForwardURL="/module/company/form")
	public ModelAndView insert(ModelAndView modelAndView, @RequestParam(required = false) Long moduleCompanyId,
			@RequestParam(required = false) String companyCode, @RequestParam(required = false) String moduleInfoIds,
			ModelMap modelMap, HttpServletRequest request) {
		modelAndView.setViewName("forward:/module/company/form");
		moduleCompanyService.insertModuleCompany(moduleInfoIds, companyCode);
		modelAndView.addObject("success", true);
		return modelAndView;

	}

	// 保存角色权限码
	@RequestMapping(value = "/company/add", method = RequestMethod.POST)
	public @ResponseBody Result save(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = false) String companyCode, @RequestParam(required = false) List<Long> modules) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		moduleCompanyService.saveCompanyModule(companyCode, modules);
		return result;
	}

}
